﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace KuPeiXun.Utility
{
    /// <summary>
    /// 序列化辅助类
    /// </summary>
    /// ===============================================================
    /// 作者：邹学良 
    /// 时间：2013-03-21
    /// 描述：创建
    /// 历史：无
    /// ===============================================================
    public class SerializeHepler<T>
    {
        #region 对list集合进行序列化成xml
        /// <summary>
        /// 对list集合进行序列化成xml
        /// </summary>
        /// <typeparam name="T">list集合中的对象的类型</typeparam>
        /// <param name="genericList">所要序列化的集合</param>
        /// <returns></returns>
        public static string Serialize(IList<T> genericList)
        {
            XmlDocument result = new XmlDocument();
            result.LoadXml("<Root></Root>");
            foreach (T obj in genericList)
            {
                XmlElement Item = result.CreateElement("Item");
                PropertyInfo[] properties = obj.GetType().GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (property.GetValue(obj, null) != null)
                    {
                        XmlElement element = result.CreateElement(property.Name);
                        element.SetAttribute("Type", property.PropertyType.Name);
                        element.InnerText = property.GetValue(obj, null).ToString();
                        Item.AppendChild(element);
                    }
                }
                result.DocumentElement.AppendChild(Item);
            }
            return result.InnerXml;
        }
        #endregion

        #region 将xml反序列化成对list集合
        /// <summary>
        /// 将xml反序列化成对list集合
        /// </summary>
        /// <typeparam name="T">list集合中的对象的类型</typeparam>
        /// <param name="xmlStr">反序列化的xml串</param>
        /// <returns></returns>
        public static List<T> Deserialize(string xmlStr)
        {
            List<T> result = new List<T>();
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.LoadXml(xmlStr);
            foreach (XmlNode ItemNode in XmlDoc.GetElementsByTagName("Root").Item(0).ChildNodes)
            {
                T item = Activator.CreateInstance<T>();
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (XmlNode propertyNode in ItemNode.ChildNodes)
                {
                    string name = propertyNode.Name;
                    string type = propertyNode.Attributes["Type"].Value;
                    string value = propertyNode.InnerXml;
                    foreach (PropertyInfo property in properties)
                    {
                        if (name == property.Name)
                        {
                            property.SetValue(item, Convert.ChangeType(value, property.PropertyType), null);
                        }
                    }
                }
                result.Add(item);
            }
            return result;
        }
        #endregion

        #region 通过序列化克隆对象
        /// <summary>
        /// 通过序列化克隆对象
        /// </summary>
        /// <param name="ObjectInstance"></param>
        /// <returns></returns>
        public static object CloneObject(T ObjectInstance)
        {

            BinaryFormatter bFormatter = new BinaryFormatter();

            MemoryStream stream = new MemoryStream();

            bFormatter.Serialize(stream, ObjectInstance);
            stream.Seek(0, SeekOrigin.Begin);
            
            return  bFormatter.Deserialize(stream) ;

        }
        #endregion

    }
}
